package com.woniu.defecttracking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.defecttracking.annotation.AddNum;
import com.woniu.defecttracking.commens.ComUtil;
import com.woniu.defecttracking.dto.RoleDto;
import com.woniu.defecttracking.dto.RolePageDto;
import com.woniu.defecttracking.dto.RuleDto;
import com.woniu.defecttracking.dto.RulePageDto;
import com.woniu.defecttracking.entity.Role;
import com.woniu.defecttracking.exception.RoleException;
import com.woniu.defecttracking.exception.RuleException;
import com.woniu.defecttracking.mapper.RoleMapper;
import com.woniu.defecttracking.param.AddRoleParam;
import com.woniu.defecttracking.param.FindRoleParam;
import com.woniu.defecttracking.param.UpdateRoleParam;
import com.woniu.defecttracking.param.UpdateRuleParam;
import com.woniu.defecttracking.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tiangle
 * @since 2021-12-31
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {


    /**
    * @作者: 倪西
    * @描述：查询所有未删除的角色信息用以授权展示
    * @时间：2022/1/7 10:47
    * @参数：[]
    * @返回：java.util.List<com.woniu.defecttracking.dto.RoleDto>
    **/
    @Override
    public List<RoleDto> findAllRole() {
        List<Role> roles = list(new QueryWrapper<Role>().eq("role_isdel", 0));
        List<RoleDto> roleDtos = ComUtil.copyList(roles, RoleDto.class);
        return roleDtos;
    }

    /**
    * @作者: 倪西
    * @描述：分页查询未删除的角色信息以及关键字查询
    * @时间：2022/1/6 15:04
    * @参数：[findRoleParam]
    * @返回：com.woniu.defecttracking.dto.RolePageDto
    **/
    @Override
    public RolePageDto findRole(FindRoleParam findRoleParam) {
        Page<Role> rolePage=null;
        if (findRoleParam.getKeyword()==null||findRoleParam.getKeyword().trim().equals("")){
            rolePage = page(new Page<Role>(findRoleParam.getPageIndex(),findRoleParam.getPageSize()),
                    new QueryWrapper<Role>().eq("role_isdel",0));
        }else{
            rolePage = page(new Page<Role>(findRoleParam.getPageIndex(),findRoleParam.getPageSize()),
                    new QueryWrapper<Role>().eq("role_isdel",0).like("role_name", findRoleParam.getKeyword()));
        }
        RolePageDto rolePageDto = new RolePageDto();
        rolePageDto.setPageIndex(rolePage.getCurrent());
        rolePageDto.setTotal(rolePage.getTotal());
        List<RoleDto> roleDtos = ComUtil.copyList(rolePage.getRecords(), RoleDto.class);
        rolePageDto.setRoleDtoList(roleDtos);
        return rolePageDto;
    }

    @Override
    public Boolean removeRole(List<Integer> ids) {
        List<Role> roles = listByIds(ids);
        if (roles.size()==ids.size()){
            for (Role role:roles){
                role.setRoleIsdel(1);
            }
            return saveOrUpdateBatch(roles);
        }else {
            return false;
        }
    }

    @Override
    public Boolean changeRole(Integer id) throws RoleException {
        Role role = getById(id);
        if (null==role){
            throw new RoleException("role不存在",2051);
        }
        if (role.getRoleStatus()==0){
            role.setRoleStatus(1);
        }else {
            role.setRoleStatus(0);
        }
        return saveOrUpdate(role);
    }
    @AddNum("roleNum")
    @Override
    public RoleDto saveRole(AddRoleParam addRoleParam) throws RoleException {
        if (null==addRoleParam.getRoleNum()||addRoleParam.getRoleNum().trim().equals("")){
            throw new RoleException("新增规则的参数异常",2051);
        }
        if (null==addRoleParam.getRoleName()||addRoleParam.getRoleName().trim().equals("")){
            throw new RoleException("新增规则的参数异常",2051);
        }
        Role role = ComUtil.copyProperties(addRoleParam, Role.class);
        role.setRoleStatus(0);//设置状态默认为0，为禁用状态
        role.setRoleIsdel(0);//设置状态默认为0，为未删除状态
        save(role);
        return ComUtil.copyProperties(role,RoleDto.class);
    }

    @Override
    public RoleDto updateRole(UpdateRoleParam updateRoleParam) throws RoleException {
        if (updateRoleParam.getRoleName()==null||updateRoleParam.getRoleNum().trim().equals("")){
            throw new RoleException("修改规则的参数异常",2073);
        }
        if (updateRoleParam.getRoleName()==null||updateRoleParam.getRoleNum().trim().equals("")){
            throw new RoleException("修改规则的参数异常",2073);
        }
        Role role = ComUtil.copyProperties(updateRoleParam, Role.class);
        saveOrUpdate(role);
        return  ComUtil.copyProperties(role, RoleDto.class);
    }
}
